package org.xserver.component.handler;
//
//import org.jboss.netty.channel.ChannelHandlerContext;
//import org.jboss.netty.channel.ChannelStateEvent;
//import org.jboss.netty.channel.Channels;
//import org.jboss.netty.channel.ExceptionEvent;
//import org.jboss.netty.channel.MessageEvent;
//import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
//import org.jboss.netty.handler.codec.http.HttpChunk;
//import org.jboss.netty.handler.codec.http.HttpMethod;
//import org.jboss.netty.handler.codec.http.HttpRequest;
//import org.jboss.netty.handler.codec.http.multipart.DefaultHttpDataFactory;
//import org.jboss.netty.handler.codec.http.multipart.DiskFileUpload;
//import org.jboss.netty.handler.codec.http.multipart.HttpDataFactory;
//import org.jboss.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
public class XServerHttpChunkHandler{}// extends SimpleChannelUpstreamHandler {
//	private static final Logger log = LoggerFactory
//			.getLogger(XServerHttpChunkHandler.class);
//
//	private boolean chunk;
//
//	private static final HttpDataFactory factory = new DefaultHttpDataFactory(
//			DefaultHttpDataFactory.MINSIZE); // Disk if size exceed MINSIZE
//
//	private HttpPostRequestDecoder decoder;
//	static {
//		/* To limit to roughly 5MB each attribute, including fileupload */
//		// factory.setMaxLimit(5000000);
//		/* should delete file on exit (in normal exit) */
//		DiskFileUpload.deleteOnExitTemporaryFile = true;
//		/* system temp directory */
//		DiskFileUpload.baseDirectory = null;
//	}
//
//	@Override
//	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
//		if (!chunk) {
//			if (decoder != null) {
//				decoder.cleanFiles();
//				decoder = null;
//			}
//
//			HttpRequest request = (HttpRequest) e.getMessage();
//			HttpMethod httpMethod = request.getMethod();
//			if (httpMethod.equals(HttpMethod.POST)
//					|| httpMethod.equals(HttpMethod.PUT)) {
//				try {
//					decoder = new HttpPostRequestDecoder(factory, request);
//				} catch (Exception ex) {
//					Channels.close(e.getChannel());
//					return;
//				}
//
//				if (request.isChunked()) {
//					chunk = true;
//				} else {
//					ctx.sendUpstream(e);
//				}
//			}
//		} else {
//			HttpChunk httpChunk = (HttpChunk) e.getMessage();
//			try {
//				decoder.offer(httpChunk);
//			} catch (Exception ex) {
//				log.error("Read http chunk error", ex);
//				Channels.close(e.getChannel());
//				return;
//			}
//			
//			readHttpDataChunkByChunk();
//		}
//	}
//	
//	private void readHttpDataChunkByChunk(){
//		try{
//			while(decoder.hasNext()){
//				
//			}
//		}
//	}
//
//	@Override
//	public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
//			throws Exception {
//		if (decoder != null) {
//			decoder.cleanFiles();
//		}
//	}
//
//	@Override
//	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
//			throws Exception {
//		log.error("xserverHttpChunk occur error", e.getCause());
//		e.getChannel().close();
//	}
//}
